//
//  TopListWindowController.m
//  iMine
//
//  Created by gaoxiang on 09-3-30.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "GameLevelTopInfo.h"
#import "ScoreClient.h"
#import "TopListViewController.h"
#import "TopListView.h"
#import "PlayerRecord.h"
#import "TopListWindowController.h"

@implementation TopListWindowController

- (id)init
{
	if (![super init])
		return nil;
	
	beginnerViewController = [[TopListViewController alloc] initWithLevelKey:@"Beginner"];
	noviceViewController = [[TopListViewController alloc] initWithLevelKey:@"Novice"];
	expertViewController = [[TopListViewController alloc] initWithLevelKey:@"Expert"];

	return self;
}

-(void)awakeFromNib
{	
	[[tabView tabViewItemAtIndex:0] setView:[beginnerViewController view]];
	[[tabView tabViewItemAtIndex:1] setView:[noviceViewController view]];
	[[tabView tabViewItemAtIndex:2] setView:[expertViewController view]];
}

- (void)windowDidBecomeMain:(NSNotification *)notification
{
	[self loadPlayerData];
}

- (void)loadPlayerData
{
	[beginnerNameField setStringValue:[[PlayerRecord instance] playerNameForBeginner]];
	[beginnerScoreField setIntValue:[[PlayerRecord instance] playerScoreForBeginner]];
	[noviceNameField setStringValue:[[PlayerRecord instance] playerNameForNovice]];
	[noviceScoreField setIntValue:[[PlayerRecord instance] playerScoreForNovice]];
	[expertNameField setStringValue:[[PlayerRecord instance] playerNameForExpert]];
	[expertScoreField setIntValue:[[PlayerRecord instance] playerScoreForExpert]];
	
	[beginnerViewController refresh];
	[noviceViewController refresh];
	[expertViewController refresh];
}

-(NSString *)windowNibName
{
	return @"TopListWindow";
}

- (IBAction)close:(id)sender
{
	[self close];
}

- (IBAction)resetLocalRecord:(id)sender
{
	[[PlayerRecord instance] setScoreForBeginner:@"Nobody" withScore:999];
	[[PlayerRecord instance] setScoreForNovice:@"Nobody" withScore:999];
	[[PlayerRecord instance] setScoreForExpert:@"Nobody" withScore:999];
	
	[self loadPlayerData];
}

- (IBAction)refresh:(id)sender
{
	[progress setHidden:NO];
	[progress startAnimation:nil];
	
	[ScoreClient getTop10FromServer];
	[beginnerViewController refresh];
	[noviceViewController refresh];
	[expertViewController refresh];
	
	[progress stopAnimation:nil];
	[progress setHidden:YES];
}

@end
